package cn.kbyue.exceptiondemo.dto;

import cn.kbyue.exceptiondemo.common.validate.Save;
import cn.kbyue.exceptiondemo.common.validate.Update;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import org.hibernate.validator.constraints.Length;
import org.springframework.format.annotation.DateTimeFormat;

import javax.validation.Valid;
import javax.validation.constraints.*;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@Data
public class LicenceDTO {

    @Min(value = 10L, groups = Update.class)
    private Long id;

    @NotNull(groups = {Save.class, Update.class})
    @Length(min = 2, max = 8)
    private String name;

    @NotNull(groups = {Save.class})
    private String typeName;

    @Future(message = "证书已过期")
    // @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "Asia/Shanghai")
    // @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime validDate;

    /**
     * 嵌套对象字段校验, 字段加 @Valid 注解, List 也生效
     * 注: 如果接口指定了 groups, 那么字段没有加对应的 groups, 将不会校验.
     * 同理: 接口校验未指定 groups, 那么字段加了 groups 也不会校验
     */
    @Valid
    @NotNull(groups = {Save.class})
    private LicenceParam licenceParam;

    /**
     * 注: 如果只有 @NotNull, 参数传 licenceParams:[]  时, 不会校验数组元素, 此时校验会通过
     *     只有数组元素 >0 才会去校验
     */
    @Valid
    @NotNull(groups = {Save.class})
    @Size(min = 1, max = 10, groups = {Save.class})
    private List<LicenceParam> licenceParams;

}
